package com.baizhi.controller;


import com.baizhi.annotation.LogAnnotion;
import com.baizhi.entity.BzAdmin;
import com.baizhi.enums.LogTypeEnum;
import com.baizhi.service.BzAdminService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xhc
 * @since 2020-09-24
 */
@RestController
@RequestMapping("admin")
public class BzAdminController {
    @Resource
    private BzAdminService bzAdminService;

    @GetMapping("login")
    @LogAnnotion(type = LogTypeEnum.LOGIN,content ="登录" )
    public Map login(BzAdmin bzAdmin, String validateCode, HttpSession session, HttpServletResponse resp) {
        System.out.println("username=" + bzAdmin.getUsername());
        System.out.println("password=" + bzAdmin.getPassword());
        Map map = new HashMap<>();
        boolean result = bzAdminService.login(bzAdmin.getUsername(), bzAdmin.getPassword());
        System.out.println(result);
        String reallCode = (String) session.getAttribute("code");
        System.out.println("用户输入的：" + validateCode + "   真实的验证码：" + reallCode);
        //判断验证码是否正确
        if (!(reallCode != null && reallCode.equalsIgnoreCase(validateCode))) {
            String errorMsg = "验证码不正确！！！";
            map.put("errMsg", errorMsg);
            return map;
        }
        if (result) {
            //登录成功、创建登录标记、存入session作用域
            session.setAttribute("bzAdmin", true);
            //1、创建cookie对象并传入参数
            //2、设置cookie的存活时间
            //3、将kookie响应给浏览器
            Cookie cookie = new Cookie("username", bzAdmin.getUsername());
            //cookie.setMaxAge(60*60*24);
            resp.addCookie(cookie);
            map.put("flag", true);
            return map;
        } else {
            map.put("flag", false);
            return map;

        }
    }

    @RequestMapping("selectAll")
    public Map selectAll(Integer page, Integer limit) {

        Page<BzAdmin> bzAdminPage = bzAdminService.selectAll(page, limit);
        Map map = new HashMap<>();
        map.put("code",0);
        map.put("msg",0);
        map.put("count",bzAdminPage.getTotal());
        map.put("data",bzAdminPage.getRecords());
        return map;
    }

    @RequestMapping("insert")
    public  Map  insert(BzAdmin    bzAdmin){
        Map map = new HashMap<>();
        try {
            bzAdminService.insert(bzAdmin);
            map.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag",false);
        }
        return map;
    };



    @RequestMapping("delete")
    public  Map   delete(Integer id){
        Map map = new HashMap<>();
        try {
            bzAdminService.delete(id);
            map.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag",false);
        }
        return map;
    }

    @RequestMapping("selectOne")
    public  BzAdmin   selectOne(Integer id){
        return bzAdminService.showOne(id);
    }

    @RequestMapping("update")
    public  Map  update(BzAdmin bzAdmin){
        Map map = new HashMap<>();
        try {
            bzAdminService.update(bzAdmin);
            map.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag",false);
        }
        return map;
    }

    @RequestMapping("deleteAll")
    public  Map  deleteAll(List<BzAdmin> ids){
        Map map = new HashMap<>();
        try {
            bzAdminService.deleteAll(ids);
            map.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag",false);
        }
        return map;
    }


}
